/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.*;

/**
 *
 * @author Gael
 */
@Entity
@NamedQueries({
    @NamedQuery(name="Users.findAll", query="select u from Users u"),
    @NamedQuery(name="Users.findById", query="select u from Users u where u.id = :userid"),
    @NamedQuery(name="Users.findVendeurs", query="select u from Users u where u.nom LIKE :key AND u.typeUser = :type")
})
public class Users implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String nom;
    private String login;
    private String pwd;
    private String typeUser;
    @OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    private Collection<Magasin> magasins = new ArrayList();
     @OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    private List<Produit> panier = new ArrayList();

    public Users() {
    }

    public Users(String nom, String login, String pwd, String type) {
        this.nom = nom;
        this.login = login;
        this.pwd = pwd;
        this.typeUser = type;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public Collection<Magasin> getMagasins() {
        return magasins;
    }

    public void setMagasins(Collection<Magasin> magasins) {
        this.magasins = magasins;
    }
    public void addMagasin(Magasin magasin) {
        this.magasins.add(magasin);
    }

    public List<Produit> getPanier() {
        return panier;
    }
    
    public void addPanier(Produit p) {
        if(!this.panier.contains(p)){
            this.panier.add(p);
        }
    }
    
    public void setPanier(List<Produit> panier) {
        this.panier = panier;
    }
    
    public String getTypeUser() {
        return typeUser;
    }
    public boolean isAcheteur(){
        if("Acheteur".equals(typeUser)){
            return true;
        }else{
            return false;
        }
    }
    
    public boolean isVendeur(){
        if("Vendeur".equals(typeUser)){
            return true;
        }else{
            return false;
        }
    }

    public void setTypeUser(String typeUser) {
        this.typeUser = typeUser;
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Users)) {
            return false;
        }
        Users other = (Users) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Entity.Users[ id=" + id + " ]";
    }
    
}
